/*-----------------------------------------------------------------------
 *  MediaFrame is an Open Source streaming media platform in Java 
 *  which provides a fast, easy to implement and extremely small applet 
 *  that enables to view your audio/video content without having 
 *  to rely on external player applications or bulky plug-ins.
 *
 *-----------------------------------------------------------------------
 *
 *  We changed a lot of code and added a lot of functionality.
 *  This includes, but not limited to, the following changes:
 *  1. The project was renamed to MediaFrame;
 *  2. The connection speed detection procedure was added;
 *  3. The JavaScript API functions were added;
 *  4. The pre and post image support functionality was added;
 *  5. The ability to save movie into the local disk was added;
 *  6. The inner buffer for a movie file was added;
 *  7. The click-through functionality was added;    
 *  8. The .zip files support was added;    
 *  9. The realtime feedback agent functionality was added.    
 *  For the full list of the current functionality please visit 
 *  the following web page: http://mediaframe.org/
 *    
 *  06 Jul 2002 - 19 Dec 2004 Konstantin Belous, Oleg Lebedev
 *
 *-----------------------------------------------------------------------
 * 4/20/01  Eliminated SampleBuffer derivation -- rolled into Obuffer -blount
 * 12/12/99  Added appendSamples() method for efficiency. MDM.
 * 15/02/99 ,Java Conversion by E.B ,ebsp@iname.com, JavaLayer
 *----------------------------------------------------------------------------- 
 * obuffer.h
 *
 *    Declarations for output buffer, includes operating system
 *   implementation of the virtual Obuffer. Optional routines
 *   enabling seeks and stops added by Jeff Tsay. 
 *   
 *
 *  @(#) obuffer.h 1.8, last edit: 6/15/94 16:51:56
 *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
 *  @(#) Berlin University of Technology
 *
 *  Idea and first implementation for u-law output with fast downsampling by
 *  Jim Boucher (jboucher@flash.bu.edu)
 *
 *  LinuxObuffer class written by
 *  Louis P. Kruger (lpkruger@phoenix.princeton.edu)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------------------
 */

package mediaframe.mpeg1.audio.decoder;

/**
 * Audio output buffer.
 */
public class Obuffer
{
    public static final int OBUFFERSIZE = 2 * 1152;	// max. 2 * 1152 samples per frame
    public static final int MAXCHANNELS = 2;		// max. number of channels

	private short[] 	buffer;
	private int[] 		bufferp;
	private int 		channels;
	private int			frequency;
  
	/**
	 * Constructor
	 */
	public Obuffer(int sample_frequency, int number_of_channels) {
		buffer = new short[OBUFFERSIZE];
		bufferp = new int[MAXCHANNELS];
		channels = number_of_channels;
		frequency = sample_frequency;

		for (int i = 0; i < number_of_channels; ++i) 
			bufferp[i] = (short)i;

	}

	public int getChannelCount() {
		return this.channels;  
	}
  
	public int getSampleFrequency() {
		return this.frequency;
	}
  
	public short[] getBuffer() {
		return this.buffer;
	}
  
	public int getBufferLength() {
		return bufferp[0];
	}
  
 	/**
	 * Takes a 16 Bit PCM sample.
	 */
	public void append(int channel, short value) {
		buffer[bufferp[channel]] = value;
		bufferp[channel] += channels;	  	
	}

    /**
     * Accepts 32 new PCM samples. 
     */
	public void appendSamples(int channel, float[] f, int len) {

	    int pos = bufferp[channel];
		short s;
		float fs;

	    for (int i = 0; i < len;) {
		  	fs = f[i++];
			fs = (fs>32767.0f ? 32767.0f 
				  : (fs < -32767.0f ? -32767.0f : fs));
			
			s = (short)fs;
			buffer[pos] = s;
			pos += channels;
	    }
		bufferp[channel] = pos;
	}

    /**
     * Write the samples to the file or directly to the audio hardware.
     */
    public void write_buffer(int val) {}
    public void close() {}

    /**
     * Clears all data in the buffer (for seeking).
     */
	public void clear_buffer() {
		for (int i = 0; i < channels; ++i) 
			bufferp[i] = (short)i;
	}
}
